package co.yixiang.modules.quartz.service.impl;


import co.yixiang.modules.quartz.service.PcProductParser;
import co.yixiang.modules.shop.domain.YxProductStockOut;
import co.yixiang.modules.shop.service.dto.YxProductStockOutDTO;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
 * 奥买家爬虫类
 */
@Service
public class AMJPcProductParser implements PcProductParser {
    //  @Value("${pc.JD_SEARCH_STR}")
    private String AMJ_SEARCH_STR = "http://search.aomygod.com/?k={searchKey}";
    //  @Value("${pc.SEARCH_KEY}")
    private String SEARCH_KEY = "{searchKey}";
    /**
     *
     * @param searchKey 商品名称
     * @param count 抓取记录数
     * @param baseProductId 外部商品基础数据配置ID（可为空）
     * @return
     */
    @Override
    public List<YxProductStockOutDTO> parserList(String searchKey, int count, Integer baseProductId) {
        List<YxProductStockOutDTO> products = new ArrayList<YxProductStockOutDTO>();
        try {
            searchKey = URLEncoder.encode(searchKey, "UTF-8").replaceAll("\\+",  "%20");
            String searchUrl = AMJ_SEARCH_STR.replace(SEARCH_KEY, searchKey);
            System.out.println("amj: " + searchUrl);
            Document doc = Jsoup.connect(searchUrl).get();
            Elements productEles = doc.getElementsByClass("cols");
            if (productEles != null) {
                int size = productEles.size();
                for (int i = 0; i < count; i++) {
                    if (i < size) {
                        Element productEle = productEles.get(i);
                        Element productA = productEle.getElementsByTag("a").get(0);
                        String url = "http://" + productA.attr("href").substring(2);
                        String imgUrl = productEle.getElementsByClass("img-error jImg").get(0).attr("data-url");
                        // String salePrice = productEle.getElementsByClass("p-price").get(0).getElementsByTag("i").get(0).text();
                        String title = productEle.getElementsByClass("img-error jImg").get(0).attr("alt");
                        String salePrice = productEle.getElementsByClass("p-normal f-l").get(0).html().substring(10);
                        // String shop = productEle.getElementsByClass("productShop-name").get(0).text();
                        YxProductStockOutDTO product = new YxProductStockOutDTO();
                        product.setTitle(title);
                        product.setPrice(salePrice);
                        product.setImage(imgUrl);
                        product.setPname(title);
                        product.setUrl(url);
                        product.setDataSource("奥买家");
                        product.setBaseProductId(baseProductId);
                        // product.setSeller(shop);
                        products.add(product);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return products;
    }

    @Override
    public List<YxProductStockOutDTO> findExternalImages(String searchKey, int count, Integer baseProductId) {
        return null;
    }




}
